﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;
using CNCSoft.EData;

namespace CNCSoft.ErpApps.TMS.longGiaoVu
{
    public partial class frmChuyenLopMoi : Form
    {
        private SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        private DataSet ds = new DataSet();
        private string _madonvi = GlobalVariable.WSInfo.CompanyID;

        public frmChuyenLopMoi()
        {
            InitializeComponent();
        }

        private void frmChuyenLop_Load(object sender, EventArgs e)
        {
            try
            {
                LoadControl();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void searchLookUpEdit_KhoaHocMoi_EditValueChanged(object sender, EventArgs e)
        {
            string makhoahoc = searchLookUpEdit_KhoaHocMoi.EditValue.ToString();
            LoadInvoiceByKhoaHocMoi(makhoahoc);
            gridControl_HocVien.DataSource = ds.Tables["TableInvoiceByKhoaHocMoi"];
        }

        private void searchLookUpEdit_KhoaHocCu_EditValueChanged(object sender, EventArgs e)
        {
            string makhoahoc = searchLookUpEdit_KhoaHocCu.EditValue.ToString();
            LoadInvoiceByKhoaHocCu(makhoahoc);
            searchLookUpEdit_HocVien.Properties.DataSource = ds.Tables["TableInvoiceByKhoaHocCu"];
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            string invoicenumber = searchLookUpEdit_HocVien.EditValue.ToString();
            InsertChuyenLop(invoicenumber, searchLookUpEdit_KhoaHocCu.EditValue.ToString(), searchLookUpEdit_KhoaHocMoi.EditValue.ToString(), 
                            Convert.ToDateTime(dateEdit_NgayChuyen.EditValue), memoEdit_LydoChuyen.EditValue.ToString());
        }

        private void btnDanhSachChuyenLop_Click(object sender, EventArgs e)
        {
            try
            {
                frmViewChuyenLop frmViewChuyen = new frmViewChuyenLop();
                frmViewChuyen.ShowDialog();
            }
            catch (Exception)
            {

                throw;
            }
        }

        #region "Coding"
        private void LoadControl()
        {
            try
            {
                dateEdit_NgayChuyen.EditValue = DateTime.Now.ToShortDateString();
                LoadKhoaHocCu(_madonvi);
                LoadKhoaHocMoi(_madonvi);
                
                searchLookUpEdit_KhoaHocCu.Properties.DataSource = ds.Tables["TableKhoaHocCu"];
                searchLookUpEdit_KhoaHocMoi.Properties.DataSource = ds.Tables["TableKhoaHocMoi"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadKhoaHocCu(string madonvi)
        {
            try
            {
                if (ds.Tables["TableKhoaHocCu"] != null)
                    ds.Tables["TableKhoaHocCu"].Clear();
                string sql = @"select * from DM_KHOAHOC where MaDonVi = '" + madonvi + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableKhoaHocCu");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadKhoaHocMoi(string madonvi)
        {
            try
            {
                if (ds.Tables["TableKhoaHocMoi"] != null)
                    ds.Tables["TableKhoaHocMoi"].Clear();
                string sql = @"select * from DM_KHOAHOC where MaDonVi = '" + madonvi + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableKhoaHocMoi");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadInvoiceByKhoaHocCu(string makhoahoc)
        {
            try
            {
                if (ds.Tables["TableInvoiceByKhoaHocCu"] != null)
                    ds.Tables["TableInvoiceByKhoaHocCu"].Clear();
                string sql = @"select InvoiceNumber, (Holot + ' ' + Ten) as HoTen from Invoice inner join Enquiry on Invoice.EnquiryNumber = Enquiry.EnquiryNumber where MaLop = '" + makhoahoc + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableInvoiceByKhoaHocCu");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadInvoiceByKhoaHocMoi(string makhoahoc)
        {
            try
            {
                if (ds.Tables["TableInvoiceByKhoaHocMoi"] != null)
                    ds.Tables["TableInvoiceByKhoaHocMoi"].Clear();
                string sql = @"select InvoiceNumber, (Holot + ' ' + Ten) as HoTen from Invoice inner join Enquiry on Invoice.EnquiryNumber = Enquiry.EnquiryNumber where MaLop = '" + makhoahoc + "'";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableInvoiceByKhoaHocMoi");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //-------------------------------Chuyển lớp-----------------------------------------

        private void InsertChuyenLop(string invoicenumber, string makhoahoccu, string makhoahocmoi, DateTime ngaychuyenlop, string lydochuyenlop)
        {
            try
            {
                string sql = @"usp_InsertChuyenLop @invoicenumber = @invnum, @makhoahoccu = @cu, @makhoahocmoi = @moi, @ngaychuyenlop = @ngay, @lydochuyenlop = @lydo";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@invnum", invoicenumber);
                cmd.Parameters.AddWithValue("@cu", makhoahoccu);
                cmd.Parameters.AddWithValue("@moi", makhoahocmoi);
                cmd.Parameters.AddWithValue("@ngay", ngaychuyenlop);
                cmd.Parameters.AddWithValue("@lydo", lydochuyenlop);
                int i;
                try
                {
                    conn.Open();
                    i = cmd.ExecuteNonQuery();
                }
                finally
                {
                    if (conn != null)
                        conn.Close();
                }
                if (i > 0)
                {
                    MessageBox.Show("Chuyển lớp thành công !!!");
                    //update malop cua invoice
                    UpdateMaLopCuaInvoice(invoicenumber, makhoahocmoi);
                    LoadInvoiceByKhoaHocMoi(makhoahocmoi);
                    gridControl_HocVien.DataSource = ds.Tables["TableInvoiceByKhoaHocMoi"];
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void UpdateMaLopCuaInvoice(string invoicenumber, string malop)
        {
            try
            {
                string sql = @"update Invoice set MaLop = '" + malop + "' where InvoiceNumber = '" + invoicenumber + "'";
                SQLHelper.ExecuteNonQuery(conn, sql);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);   
            }
        }
        #endregion
    }
}
